package com.yeung.exam;

import java.util.Scanner;

/*
1
0 0 0 2
0 0 0 2
0 0 4 8
0 0 4 8

思路:
每次操作一行或者一列,
使用两个指针,low指向最底层(仅有交换操作,或者合并且合并值不为0,指针才移动), row每次往前移动一次
 */
public class 游戏2048 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int code = sc.nextInt();
        int[][] mat = new int[4][4];
        int index = 0;
        while (index < 4) {
            for (int i = 0; i < 4; i++) {
                mat[index][i] = sc.nextInt();
            }
            index++;
        }
        //开始计算
        switch (code) {
            case 1://上移动
                for (int col = 0; col < 4; col++) {
                    int low = 0;
                    for (int row = 1; row < 4; row++) {//不处理最后一行
                        if (mat[low][col] == mat[row][col]) {
                            //相等,要消去  处理下一行
                            mat[low][col] = mat[low][col] * 2;
                            mat[row][col] = 0;
                            if (mat[low][col] != 0) {
                                low++;
                            }
                        } else if (mat[low][col] == 0 && mat[row][col] != 0) {
                            //非0值往上交换
                            mat[low][col] = mat[row][col];
                            mat[row][col] = 0;
                            //  low++;//交换 不用++
                        } else if (mat[row][col] != 0) {
                            if (row - low == 1) {
                                continue;
                            } else {
                                mat[low - 1][col] = mat[row][col];
                                mat[row][col] = 0;
                                low++;
                            }
                        } else {
                            mat[low + 1][col] = mat[row][col];
                            mat[row][col] = 0;
                            low++;
                        }
                    }
                }
                break;
            case 2://下移动
                for (int col = 0; col < 4; col++) {
                    int low = 3;
                    for (int row = 2; row >= 0; row--) {//不处理最后一行
                        if (mat[low][col] == mat[row][col]) {
                            //相等,要消去  处理下一行
                            mat[low][col] = mat[low][col] * 2;
                            mat[row][col] = 0;
                            if (mat[low][col] != 0) {
                                low--;
                            }
                        } else if (mat[low][col] == 0 && mat[row][col] != 0) {
                            //非0值往下交换
                            mat[low][col] = mat[row][col];
                            mat[row][col] = 0;
                            //  low++;//交换 不用++
                        } else if (mat[row][col] != 0) {
                            if (low - row == 1) {
                                continue;
                            } else {
                                mat[low - 1][col] = mat[row][col];
                                mat[row][col] = 0;
                                low++;
                            }
                        } else {
                            mat[low - 1][col] = mat[row][col];
                            mat[row][col] = 0;
                            low--;
                        }
                    }
                }
                break;
            case 3://左移动
                for (int row = 0; row < 4; row++) {
                    int low = 0;
                    for (int col = 1; col < 4; col++) {//不处理最后一行
                        if (mat[row][low] == mat[row][col]) {
                            //相等,要消去  处理下一行
                            mat[row][low] = mat[row][col] * 2;
                            mat[row][col] = 0;
                            if (mat[row][low] != 0) {
                                low++;
                            }
                        } else if (mat[row][low] == 0) {
                            //非0值往左交换
                            mat[row][low] = mat[row][col];
                            mat[row][col] = 0;
                            //  low++;//交换 不用++
                        } else if (mat[row][col] != 0) {
                            if (col - low == 1)//xiangling bu交换
                                continue;
                            else {
                                mat[row][low+1] = mat[row][col];
                                mat[row][col] = 0;
                                low++;
                            }
                        } else {
          /*                  mat[row][low + 1] = mat[row][col];
                            mat[row][col] = 0;*/
                            low++;
                        }
                    }
                }
                break;
            default://右移动
                for (int row = 0; row < 4; row++) {
                    int low = 3;
                    for (int col = 2; col >= 0; col--) {//不处理最后一行
                        if (mat[row][low] == mat[row][col]) {
                            //相等,要消去  处理下一行
                            mat[row][low] = mat[row][col] * 2;
                            mat[row][col] = 0;
                            if (mat[row][low] != 0) {
                                low--;
                            }
                        } else if (mat[row][low] == 0) {
                            //非0值往右交换
                            mat[row][low] = mat[row][col];
                            mat[row][col] = 0;
                            //  low++;//交换 不用++
                        } else if (mat[row][col] != 0) {
                            if (low - col == 1)//xiangling bu交换
                                continue;
                            else {
                                mat[low - 1][col] = mat[row][col];
                                mat[row][col] = 0;
                                low--;
                            }
                        } else {
                            low--;
                        }
                    }
                }
                break;
        }
        for (int[] ints : mat) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

    }

}
